package cn.firecode.data_collect.controller;


import cn.firecode.data_collect.domain.entity.CollectDrivepipeT;
import cn.firecode.data_collect.service.CollectDrivepipeTService;
import cn.firecode.data_collect.utils.DataResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import lombok.extern.slf4j.XSlf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/drivepipe")
@Slf4j
public class CollectDrivepipeTController {

    @Autowired
    private CollectDrivepipeTService drivepipeTService;

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public DataResponse add(@RequestParam("length") Double length, @RequestParam("diameter") Double diameter,
                            String describe){

        QueryWrapper<CollectDrivepipeT> wrapper = new QueryWrapper<>();
        wrapper.eq("length",length).eq("diameter",diameter).eq("is_delete",0);
        CollectDrivepipeT one = drivepipeTService.getOne(wrapper);
        if(one!=null){
            return DataResponse.failed(1,"新增套管错误，该参数套管已经存在");
        }

        CollectDrivepipeT drivepipeT = new CollectDrivepipeT();
        drivepipeT.setCreateTime(LocalDate.now());
        drivepipeT.setDescribeInfo(describe);
        drivepipeT.setDiameter(diameter);
        drivepipeT.setLength(length);
        drivepipeT.setIsDelete(0);
        boolean saveReslut = drivepipeTService.save(drivepipeT);
        if(saveReslut){
            log.info("新增套管信息"+drivepipeT.toString());
            return DataResponse.succeed("新增成功");
        }else{
            return DataResponse.failed(1, "新增失败");
        }
    }

    @RequestMapping(value = "/update/{drivepipeId}", method = RequestMethod.POST)
    public DataResponse update(@PathVariable("drivepipeId") Integer drivepipeId,  @RequestParam("length") Double length,
                               @RequestParam("diameter") Double diameter,
                               String describe){
        CollectDrivepipeT collectDrivepipeT = drivepipeTService.getById(drivepipeId);
        if(collectDrivepipeT==null){
            return DataResponse.failed(1,"更新失败，无有效记录");
        }

        collectDrivepipeT.setLength(length);
        collectDrivepipeT.setDiameter(diameter);
        collectDrivepipeT.setDescribeInfo(describe);

        boolean result = drivepipeTService.saveOrUpdate(collectDrivepipeT);
        if(result){
            log.info("更新套管信息成功:"+collectDrivepipeT.toString());
            return DataResponse.succeed("更新成功");
        }else{
            return DataResponse.failed(1,"更新失败");
        }


    }

    @RequestMapping(value = "/delete/{drivepipeId}", method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("drivepipeId") Integer drivepipeId){
        CollectDrivepipeT drivepipeT = drivepipeTService.getById(drivepipeId);
        if(drivepipeId==null){
            return DataResponse.failed(1, "删除失败,无有效记录");
        }
        drivepipeT.setIsDelete(1);
        boolean result = drivepipeTService.saveOrUpdate(drivepipeT);
        if(result){
            log.info("删除套管信息成功"+drivepipeT.toString());
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }
    }

    @RequestMapping(value = "/getList" , method = RequestMethod.GET)
    public DataResponse getList() {
        QueryWrapper<CollectDrivepipeT> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<CollectDrivepipeT> list = drivepipeTService.list(wrapper);
        return DataResponse.succeed(String.valueOf(list.size()), list);


    }


    @RequestMapping(value = "/get/{drivepipeId}" , method = RequestMethod.GET)
    public DataResponse getInfo(@PathVariable("drivepipeId")Integer drivepipeId){

        CollectDrivepipeT byId = drivepipeTService.getById(drivepipeId);
        if(byId==null){
            return DataResponse.failed(1,"查询失败");
        }
        return DataResponse.succeed("查询成功",byId);

    }
}
